<template>
  <div class="tp-box">
    <a-drawer
      :visible="props.visible"
      :width="props.width"
      :closable="props.closable"
      :destroyOnClose="props.destroyOnClose"
      @close="handleClose"
    >
      <template #title>
        <div class="title-detail" style="float: left;">
          <Tooltip placement="top">
            <template #title>
              <span>{{ getTipTitle }}</span>
            </template>
            <h3>{{ getTitle }}</h3>
          </Tooltip>
        </div>
        <div style="float: right">
          <a-space style="float: right; margin-right: 10px">
            <a-button type="primary" @click="handleExport"> 导出 </a-button>
          </a-space>
          &nbsp;&nbsp;&nbsp;&nbsp;
        </div>
      </template>

      <!-- :scroll="{ x: 1500, y: 600 }" -->
      <a-table
        :columns="computedTableColumn"
        :data-source="computedTableData"
        :page-size-options="pageSizeOptions"
        :loading="loading"
        bordered
        :pagination="false"
        size="middle"
        :scroll="{ x: 'calc(700px + 50%)', y: props.detailTableHeight }"
      >
        <!-- <template #action>
          <a>action</a>
        </template> -->
      </a-table>
      <div class="pagination">
        <a-pagination
          show-size-changer
          v-model:current="pageIndex"
          v-model:pageSize="pageSize"
          :total="total"
          @show-size-change="onShowSizeChange"
        />
      </div>
    </a-drawer>
    <VillageDetail
      v-model:vvisible="vvisible"
      title="抽屉标题"
      :width="1500"
      :detailTableHeight="detailTableHeight"
      :vclosable="true"
      :vdestroyOnClose="true"
      :data="detailData"
      :vdetailParams="vdetailParams"
      :vcurrentTitle="v_currentTitle"
      @close="handleVillageClose"
    >
      <!-- <p>抽屉内容</p> -->
    </VillageDetail>
    <hourseDetail
        v-model:visible="h_visible"
        title="抽屉标题"
        :width="1500"
        :detailTableHeight="detailTableHeight"
        :closable="true"
        :destroyOnClose="true"
        :data="detailData"
        :detailParams="h_detailParams"
        :currentqx="h_currentqx"
        :currentTitle="h_currentTitle"
        @close="handleHClose"
      >
        <!-- <p>抽屉内容</p> -->
      </hourseDetail>
  </div>
</template>
<script setup lang="ts">
  import moment from 'moment';
  import dayjs from 'dayjs';
  import { Tooltip } from 'ant-design-vue';
  import { ref, computed, onMounted, watch } from 'vue';
  import { getTownshipDangerList } from '/@/api/demo/system';
  import VillageDetail from '../component/detail.vue';
  import hourseDetail from '../component/hourseDetail.vue';
  import { useGlobSetting } from '/@/hooks/setting';
  import { useUserStore } from '/@/store/modules/user';
  import { downloadByUrl } from '/@/utils/file/download';
  import { Row } from 'ant-design-vue/lib/grid';
  //   import Detail from './component/detailDrawer.vue';
  const userStore = useUserStore();
  const globSetting = useGlobSetting();
  // 表格的 ref Dom名称
  const visible = ref(false);
  const vvisible = ref(false);
  const h_visible = ref(false);
  let detailData = ref<any[]>([]);
  let vdetailParams = ref({}); // 详情参数
  let detailTableHeight = ref<number>(0); // 表格高度
  let v_currentTitle = ref({});
  let h_detailParams = ref({}); // 详情参数
  let h_currentqx = ref<string>('');
  let h_currentTitle = ref<string>('');
  // 默认传过来的值q
  const props = defineProps({
    visible: {
      type: Boolean,
      default: true,
    },
    title: {
      type: String,
      default: '',
    },
    width: {
      type: Number,
      default: 256,
    },
    detailTableHeight: {
      type: Number,
      default: 600,
    },
    hzfs: {
      type: String,
      default: '',
    },
    closable: {
      type: Boolean,
      default: true,
    },
    data: {
      type: Array,
      default: () => [],
    },
    detailParams: {
      type: Object,
      default: () => {},
    },
    currentXzqdmname: {
      type: String,
      default: '',
    },
    currentTitle: {
      type: String,
      default: '',
    },
    wscolsList: {
      type: Array,
      default: () => [{ wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }],
    },
    destroyOnClose: {
      type: Boolean,
      default: true,
    },
    onClose: {
      type: Function as PropType<() => void>,
      default: () => {},
    },
  });
  let pageIndex = ref(1);
  let pageSize = ref(100);
  let loading = ref(false);
  const pageSizeOptions = ref<string[]>(['100', '200', '300']);
  let tableSource = ref<any>([]);
  let total = ref(0);
  const columns = [
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      align: 'center',
      width: 80,
    },
    {
      title: '名称',
      dataIndex: 'town',
      key: 'town',
      align: 'center',
      width: 100,
      scopedSlots: { customRender: 'town' },
      ellipsis: true, //需ant版本为1.5及以上
      customCell: (row, index, column) => {
        let isNoHzyh = row.xzq !== '汇总' ? true : false;
        let style = 'cursor:pointer;text-decoration: underline; color: blue;';
        let isNoZero = row[column.key] !== 0 ? true : false;
        return {
          style: `${isNoHzyh === true && isNoZero ? style : ''}`,
          class: 'cell-hover',
          onClick: async () => {
            getDetailData(row, index, column, isNoHzyh, isNoZero);
          },
        };
      },
    },
    {
      title: '房屋总数(栋)',
      dataIndex: 'zcount',
      key: 'zcount',
      align: 'center',
      width: 120,
    },
    {
      title: '居住人口数',
      dataIndex: 'jzrks',
      key: 'jzrks',
      align: 'center',
      width: 120,
    },
    // 居住 商业
    {
      title: '租住人口数',
      dataIndex: 'zzrs',
      key: 'zzrs',
      align: 'center',
      width: 120,
    },
    {
      title: '房间数量',
      dataIndex: 'fjsl',
      key: 'fjsl',
      align: 'center',
      width: 100,
    },
    {
      title: '出租房间数量',
      dataIndex: 'jzfjs',
      align: 'center',
      width: 120,
    },
    {
      title: '一层房屋栋数',
      dataIndex: 'lc_dscount',
      key: 'lc_dscount',
      align: 'center',
      width: 120,
    },
    {
      title: '两层房屋栋数',
      dataIndex: 'lcjzsl',
      key: 'lcjzsl',
      align: 'center',
      width: 120,
    },
    {
      title: '三层及以上房屋栋数',
      dataIndex: 'csjyssl',
      key: 'csjyssl',
      align: 'center',
      width: 160,
    },
    {
      title: '满足两条疏散路径数量',
      dataIndex: 'ssltdbfjsl',
      key: 'ssltdbfjsl',
      align: 'center',
      width: 180,
    },
    {
      title: '不满足两条疏散路径数量',
      dataIndex: 'no_ssltdbfjsl',
      key: 'no_ssltdbfjsl',
      align: 'center',
      width: 200,
      customCell: (row, index, column) => {
        let isNoHzyh = row.xzq !== '汇总' ? true : false;
        let style = 'cursor:pointer;text-decoration: underline; color: blue;';
        let isNoZero = row[column.key] !== 0 ? true : false;
        return {
          style: `${isNoHzyh === true && isNoZero ? style : ''}`,
          class: 'cell-hover',
          onClick: async () => {
            getDetailData_1(row, index, column, isNoHzyh, isNoZero);
          },
        };
      },
    },
    {
      title: '正在施工改造数量',
      dataIndex: 'zzsggz',
      key: 'zzsggz',
      align: 'center',
      width: 150,
      customCell: (row, index, column) => {
        let isNoHzyh = row.xzq !== '汇总' ? true : false;
        let style = 'cursor:pointer;text-decoration: underline; color: blue;';
        let isNoZero = row[column.key] !== 0 ? true : false;
        return {
          style: `${isNoHzyh === true && isNoZero ? style : ''}`,
          class: 'cell-hover',
          onClick: async () => {
            getDetailData_2(row, index, column, isNoHzyh, isNoZero);
          },
        };
      },
    },
    {
      title: '正在制定方案数量',
      dataIndex: 'zzzdfa',
      key: 'zzzdfa',
      align: 'center',
      width: 150,
      customCell: (row, index, column) => {
        let isNoHzyh = row.xzq !== '汇总' ? true : false;
        let style = 'cursor:pointer;text-decoration: underline; color: blue;';
        let isNoZero = row[column.key] !== 0 ? true : false;
        return {
          style: `${isNoHzyh === true && isNoZero ? style : ''}`,
          class: 'cell-hover',
          onClick: async () => {
            getDetailData_3(row, index, column, isNoHzyh, isNoZero);
          },
        };
      },
    },
    {
      title: '现有防护网数量',
      dataIndex: 'ckfhwggptzlsl',
      key: 'ckfhwggptzlsl',
      align: 'center',
      width: 150,
    },
    {
      title: '拆除防护网数量',
      dataIndex: 'ccfhwggptzlsl',
      key: 'ccfhwggptzlsl',
      align: 'center',
      width: 150,
    },
    {
      title: '现有电动自行车数量',
      dataIndex: 'bdfwjzrydddzxcsl',
      key: 'bdfwjzrydddzxcsl',
      align: 'center',
      width: 170,
    },
    {
      title: '灭火器数量',
      dataIndex: 'mhqsl',
      key: 'mhqsl',
      align: 'center',
      width: 130,
    },
      // 商业
    {
      title: '防烟逃生面罩数量',
      dataIndex: 'fytsmz',
      key: 'fytsmz',
      align: 'center',
      width: 170,
    },
    {
      title: '悬挂固定警示标牌栋数',
      dataIndex: 'xggdjsbpds',
      key: 'xggdjsbpds',
      align: 'center',
      width: 190,
    },
    {
      title: '未完成悬挂固定警示标牌栋数',
      dataIndex: 'no_xggdjsbpds',
      key: 'no_xggdjsbpds',
      align: 'center',
      width: 230,
      customCell: (row, index, column) => {
        let isNoHzyh = row.xzq !== '汇总' ? true : false;
        let style = 'cursor:pointer;text-decoration: underline; color: blue;';
        let isNoZero = row[column.key] !== 0 ? true : false;
        return {
          style: `${isNoHzyh === true && isNoZero ? style : ''}`,
          class: 'cell-hover',
          onClick: async () => {
            getDetailData_4(row, index, column, isNoHzyh, isNoZero);
          },
        };
      },
    },
    {
      title: '房屋烟感报警器数量',
      dataIndex: 'jzsyjyplmhxts',
      key: 'jzsyjyplmhxts',
      align: 'center',
      width: 190,
    },
    {
      title: '使用易燃可燃夹芯彩钢板搭建房屋栋数',
      dataIndex: 'wdwhjgfjgqsyyrkrgbdj',
      key: 'wdwhjgfjgqsyyrkrgbdj',
      align: 'center',
      width: 280,
    },
    {
      title: '疏散通道使用易燃可燃材料装修房屋栋数',
      dataIndex: 'sstdsyyrkrclzx',
      key: 'sstdsyyrkrclzx',
      align: 'center',
      width: 290,
    },
    {
      title: '不利于烟气扩散的顶棚南棚等房屋栋数',
      dataIndex: 'gryssdcsszblyyqksddp',
      key: 'gryssdcsszblyyqksddp',
      align: 'center',
      width: 280,
    },

  ];

  // 计算总数的方法
  function vtotal(arr: any, item: any) {
    let total = arr.reduce((pre: any, cur: any) => {
      return parseInt(pre) + parseInt(cur[item]);
    }, 0);
    return total;
  }
  // 计算属性，用于实时更新数据到页面上
  const computedTableColumn = computed(() => {
    let newColumn: any = [];
    newColumn = columns;
    return newColumn;
  });
  const computedTableData = computed(() => {
    console.log(tableSource.value);
    return tableSource.value;
  });
  const getTitle = computed(() => {
    // let str = `【${props.currentXzqdmname} 】-- ${props.currentTitle}`;
    let str = `【${props.currentTitle}】的基本情况统计`;
    if (str.length > 50) {
      str = str.substring(0, 50) + '...';
    }
    return str;
  });
  const getTipTitle = computed(() => {
    let str = `${props.currentTitle}`;
    return str;
  });
  watch(
    () => props.visible,
    (newVal, oldVal) => {
      // console.log(`visible 变化了，新值：${newVal}，旧值：${oldVal}`);
      // 在这里执行你需要的操作
      if (newVal) {
        getTableData();
      }
    },
  );
  const getTableData = async () => {
    let params: any = {
      pageindex: pageIndex.value,
      pagesize: pageSize.value,
      ...props.detailParams,
    };
    loading.value = true;
    console.log('@乡镇参数', params);
    let res = await getTownshipDangerList(params);
    loading.value = false;
    let bullding_count = vtotal(res.items, 'zcount');
    let jzrks_count = vtotal(res.items, 'jzrks');
    let zzrs_count = vtotal(res.items, 'zzrs');
    let index_count = res.items.length + 1;
    let three_cs_count = vtotal(res.items, 'csjyssl');
    let two_cs_count = vtotal(res.items, 'lcjzsl');
    let fjsl_count = vtotal(res.items, 'fjsl');
    let jzfjs_count = vtotal(res.items, 'jzfjs');
    let ywc_sstd_count = vtotal(res.items, 'sstdywc');
    let wcc_sstd_zdfa_count = vtotal(res.items, 'zzzdfa');
    let wcc_sstd_sgzg_count = vtotal(res.items, 'zzsggz');
    let ckfhwggptzlsl_count = vtotal(res.items, 'ckfhwggptzlsl');
    let bdfwjzrydddzxcsl_count = vtotal(res.items, 'bdfwjzrydddzxcsl');
    let mhqsl_count = vtotal(res.items, 'mhqsl');
    let fytsmz_count = vtotal(res.items, 'fytsmz');
    let xggdjsbp_count = vtotal(res.items, 'xggdjsbp');
    let jzsyjyplmhxts_count = vtotal(res.items, 'jzsyjyplmhxts');
    // 商业
    let jzszlwxdlgybjqs_count = vtotal(res.items, 'jzszlwxdlgybjqs');
    let wdwhjgfjgqsyyrkrgbdj_count = vtotal(res.items, 'wdwhjgfjgqsyyrkrgbdj');
    let gryssdcsszblyyqksddp_count = vtotal(res.items, 'gryssdcsszblyyqksddp');
    let lc_dscount = vtotal(res.items, 'lc_dscount');
    let ssltdbfjsl = vtotal(res.items, 'ssltdbfjsl');
    let no_ssltdbfjsl = vtotal(res.items, 'no_ssltdbfjsl');
    let no_xggdjsbpds = vtotal(res.items, 'no_xggdjsbpds');
    let xggdjsbpds = vtotal(res.items, 'xggdjsbpds');
    let sstdsyyrkrclzx = vtotal(res.items, 'sstdsyyrkrclzx');
    let allObj = {
      xzq: '汇总',
      zcount: bullding_count,
      jzrks: jzrks_count,
      zzrs: zzrs_count,
      index: index_count,
      csjyssl: three_cs_count,
      lcjzsl: two_cs_count,
      fjsl: fjsl_count,
      jzfjs: jzfjs_count,
      sstdywc: ywc_sstd_count,
      zzzdfa: wcc_sstd_zdfa_count,
      zzsggz: wcc_sstd_sgzg_count,
      ckfhwggptzlsl: ckfhwggptzlsl_count,
      bdfwjzrydddzxcsl: bdfwjzrydddzxcsl_count,
      fytsmz: fytsmz_count,
      xggdjsbp: xggdjsbp_count,
      jzsyjyplmhxts: jzsyjyplmhxts_count,
      jzszlwxdlgybjqs: jzszlwxdlgybjqs_count,
      wdwhjgfjgqsyyrkrgbdj: wdwhjgfjgqsyyrkrgbdj_count,
      sstdsyyrkrclzx: sstdsyyrkrclzx,
      gryssdcsszblyyqksddp: gryssdcsszblyyqksddp_count,
      mhqsl: mhqsl_count,
      lc_dscount: lc_dscount,
      ssltdbfjsl: ssltdbfjsl,
      no_ssltdbfjsl: no_ssltdbfjsl,
      no_xggdjsbpds: no_xggdjsbpds,
      xggdjsbpds: xggdjsbpds,
    };

    let list = res.items;
    tableSource.value = list;
    tableSource.value.push(allObj);
    total.value = res.total;

    // console.log('@resqqq', list);
  };
  //  调用 父组件传过来的方法
  const emit = defineEmits(['close']);
  // 导出
  const getDetailData = async (
    row: any,
    index: number,
    column: any,
    isNoHzyh: boolean,
    isNoZero: boolean,
  ) => {
    if (isNoHzyh && isNoZero) {
      let params: any = {
        towndm: row.towndm,
        ...props.detailParams,
      };
      console.log(
      '************************************************@@@@@********************************',
        row,
    );
      vdetailParams.value = params;
      let current: any = {
        title: row.xzq + '-' + row.town,
        xzq: row.xzq,
      };
      v_currentTitle.value = current;
      showDrawer();
    }
  };
//获取当前区不满足两条疏散路径的房屋
const getDetailData_1 = async (
    row: any,
    index: number,
    column: any,
    isNoHzyh: boolean,
    isNoZero: boolean,
  ) => {
    if (isNoHzyh && isNoZero) {
     
      let params = {
        xzqdm: row.xzqdm,
        towndm: row.towndm,
        category: '1',
        titlename: '不满足两条疏散路径的房屋',
		    startTime: props.detailParams.startTime,
        endTime: props.detailParams.endTime,
      };
      h_detailParams.value = params;
      console.log('&&&&', h_detailParams);
      h_currentTitle.value = row.xzq;
      showHDrawer();
    }
  };
  //正在施工改造数量
  const getDetailData_2 = async (
    row: any,
    index: number,
    column: any,
    isNoHzyh: boolean,
    isNoZero: boolean,
  ) => {
    if (isNoHzyh && isNoZero) {
      let params = {
        xzqdm: row.xzqdm,
        towndm: row.towndm,
        category: '2',
        titlename: '正在施工改造的房屋',
        startTime: props.detailParams.startTime,
        endTime: props.detailParams.endTime,
      };
      h_detailParams.value = params;
      h_currentTitle.value = row.xzq;
      showHDrawer();
    }
  };
   //正在制定方案数量
  const getDetailData_3 = async (
    row: any,
    index: number,
    column: any,
    isNoHzyh: boolean,
    isNoZero: boolean,
  ) => {
    if (isNoHzyh && isNoZero) {
      let params = {
        xzqdm: row.xzqdm,
        towndm: row.towndm,
        category: '3',
        titlename: '正在制定方案的房屋',
        startTime: props.detailParams.startTime,
        endTime: props.detailParams.endTime,
      };
      h_detailParams.value = params;
      h_currentTitle.value = row.xzq;
      showHDrawer();
    }
  };
  //未完成悬挂固定警示标牌栋数
  const getDetailData_4 = async (
    row: any,
    index: number,
    column: any,
    isNoHzyh: boolean,
    isNoZero: boolean,
  ) => {
    if (isNoHzyh && isNoZero) {
      let params = {
        xzqdm: row.xzqdm,
        towndm: row.towndm,
        category: '4',
        titlename: '未完成悬挂固定警示标牌的房屋',
        startTime: props.detailParams.startTime,
        endTime: props.detailParams.endTime,
      };
      h_detailParams.value = params;
      h_currentTitle.value = row.xzq;
      showHDrawer();
    }
  };
  function showHDrawer() {
    
    h_visible.value = true;
    vvisible.value = false;
    console.log('不满足两条', h_visible.value);
  }
  function handleHClose()
  {
   // emit('close');
    h_visible.value = false;
   // vvisible.value = false;
  }
  // 打开抽屉
  function showDrawer() {
    vvisible.value = true;
    h_visible.value = false;
  }
  function handleClose() {
    emit('close');
    vvisible.value = false;
    h_visible.value = false;
  }
  function handleExport() {
    const uriBase = `${globSetting.apiBaseUrl}${globSetting.apiUrl}`;
    const tk = userStore.getToken;
    downloadByUrl({
      url:
        uriBase +
        '/houses/evolvestatic/ExportExcel?access_token=' +
        tk +
        '&xzqdm=' +
        props.detailParams.xzqdm +
        '&startTime=' +
        props.detailParams.startTime +
        '&endTime=' +
        props.detailParams.endTime,
      target: '_blank',
    });
  }
  const handleVillageClose = () => {
    vvisible.value = false;
  };
  // 切换页码的时候更新  pageIndex 和  pageSize
  const onShowSizeChange = (pageIndex: number, pageSize: number) => {
    // antDesign  自动给  pageIndex 和 pageSize 赋值
    // pageIndex.value = pageIndex;
    // pageSize.value = pageSize;
    // console.log('@pageIndex,pageSize', pageIndex, pageSize);
  };
  watch(pageSize, () => {
    // console.log('pageSize', pageSize.value);
    getTableData();
  });
  watch(pageIndex, () => {
    // console.log('current', pageIndex.value);
    getTableData();
  });
  onMounted(() => {
    getTableData();
  });
</script>
<style lang="less" scoped>
  .tp-box {
    height: 100%;
  }
  .title-detail {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
  .pagination {
    margin-top: 5px;
    display: flex;
    justify-content: flex-end;
  }
</style>
